<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>末地传送门坐标计算</title>
    <script src="../footer.js"></script>
</head>
<body>
    <h1>末地传送门坐标计算v0.4</h1>
    <p>
    第一次扔的坐标：x<sub>1</sub>=<input id="x1" class="coordinate" type="text"> ,
    y<sub>1</sub>=<input id="y1" class="coordinate" type="text"><br>
    第一次落地坐标：x<sub>2</sub>=<input id="x2" class="coordinate" type="text"> ,
    y<sub>2</sub>=<input id="y2" class="coordinate" type="text"><br>
    第二次扔的坐标：x<sub>3</sub>=<input id="x3" class="coordinate" type="text"> ,
    y<sub>3</sub>=<input id="y3" class="coordinate" type="text"><br>
    第二次落地坐标：x<sub>4</sub>=<input id="x4" class="coordinate" type="text"> ,
    y<sub>4</sub>=<input id="y4" class="coordinate" type="text"><br>
    <input type="button" value="计算末地门坐标" onclick="javascript:calculate();">
    </p>
    <div id="answer"></div>
    <p style="text-align: right;"><a style="text-decoration:none;color: rgba(0, 0, 0, 0.5);" href="/">by 小喾苦 2021-7-5</a></p>
    <script>
var x1_input = document.getElementById('x1');
var y1_input = document.getElementById('y1');
var x2_input = document.getElementById('x2');
var y2_input = document.getElementById('y2');
var x3_input = document.getElementById('x3');
var y3_input = document.getElementById('y3');
var x4_input = document.getElementById('x4');
var y4_input = document.getElementById('y4');
var answer_div = document.getElementById('answer');
//alert(parseFloat(x1.value));
function show_answer(answer_str){
    answer_div.innerHTML = answer_str;
    //alert(answer_str);
}

function add_answer(x, y){
    var answer_str = "";
    x = Math.round(x);
    y = Math.round(y);
    answer_str += "末地门坐标： x="+x+ "，y=" +y + "<br>";
    //x = x.toFixed(0);
    //y = y.toFixed(0);
    answer_str += "指令： /tp " +x+ " ~ " +y+ "<br>";
    return answer_str;
}

function calculate(){
    var answer_str = "<p>";
    var k1 = 0;
    var k2 = 0;
    var b1 = 0;
    var b2 = 0;
    var x = 0;
    var y = 0;
    var x1 = parseFloat(x1_input.value);
    var y1 = parseFloat(y1_input.value);
    var x2 = parseFloat(x2_input.value);
    var y2 = parseFloat(y2_input.value);
    var x3 = parseFloat(x3_input.value);
    var y3 = parseFloat(y3_input.value);
    var x4 = parseFloat(x4_input.value);
    var y4 = parseFloat(y4_input.value);
    if(x1 == x2 && x3 == x4){answer_str += "<font color='red'>错误！ 两次扔时，不能在同一直线上！</font><br>";}
    else if(x1 == x2){
        k2 = (y3 - y4) / (x3 - x4);
        b2 = y3 - k2 * x3;
        x = x1;
        y = k2 * x1 + b2;
        answer_str += add_answer(x, y); 
    }else if(x3 == x4){
        k1 = (y1 - y2) / (x1 - x2);
        b1 = y1 - k1 * x1;
        x = x3;
        y = k1 * x3 + b1;
        answer_str += add_answer(x, y); 
    }else if( (y1 - y2) / (x1 - x2) == (y3 - y4) / (x3 - x4) ){
        answer_str += "<font color='red'>错误！ 两次扔时，不能在同一直线上！</font><br>";
    }else{
        k1 = (y1 - y2) / (x1 - x2);
        b1 = y1 - k1 * x1;
        k2 = (y3 - y4) / (x3 - x4);
        b2 = y3 - k2 * x3;
        x = (b2 - b1) / (k1 - k2);
        y = k1 * x + b1;
        answer_str += add_answer(x, y);
    }
    answer_div.innerHTML = answer_str + "</p>"
    /*
    try{
        if(x1 == x2){answer_str += "错误！ x1 、 x2 不能相等！<br>";}
        if(x3 == x4){answer_str += "错误！ x3 、 x4 不能相等！<br>";}
        k1 = (y1 - y2) / (x1 - x2);
        b1 = y1 - k1 * x1;
        k2 = (y3 - y4) / (x3 - x4);
        b2 = y3 - k2 * x3;
        if(k1 == k2){answer_str += "错误！ 两次扔时，不能在同一直线上！<br>";}
        x = (b2 - b1) / (k1 - k2);
        y = k1 * x + b1;
        answer_str += "末地门坐标： "+x+ "，" +y + "<br>";
        answer_str += "指令： /tp " +x+ " " +y+ " ~";
    }catch(e){
        answer_str += "计算出错！请检查坐标输入是否正确！<br>";
    }
    show_answer(answer_str)*/
}
    </script>
    <style>
        .coordinate{
            width: 30px;
        }
    </style>
</body>
</html>
